﻿Imports System.IO

Public Class Frm_mowadafin
    Public avo As Employe
    Dim loc As String = "photo.jpg"
    Dim bol As Boolean = True

    Private Sub Btn_parc_halaijtima_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Btn_parc_wadifa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Enabled = False
        Frm_Avoc_Bure_wadifa.txt_wadifa.Select()
        Frm_Avoc_Bure_wadifa.Show()
    End Sub

    Private Sub Btn_ajou_tele_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou_tele.Click
        Partager.typePhone = "Tel"
        Me.Enabled = False
        Frm_Tele_Bure.txt_tele.Select()

        '------
        If List_tous.SelectedIndices.Count > 0 Then
            Try
                Frm_Tele_Bure.List_tele.Items.Clear()
                Partager.list_hatif.Clear()
                For Each numhone As Phone In avo.list_phone
                    Frm_Tele_Bure.List_tele.Items.Add(New ListViewItem(New String() {numhone.Num}))
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

        End If
        Frm_Tele_Bure.Show()
    End Sub

    Private Sub Btn_parc_ville_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_ville.Click
        Me.Enabled = False
        Frm_Avoc_Bure_ville.txt_ville.Select()
        Frm_Avoc_Bure_ville.Show()
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click
        Me.Enabled = False
        Frm_Rech_mowadafin.Show()
    End Sub

    Private Sub Frm_mowadafin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txt_3a2ili.Select()
        comb_hala_ijtim.SelectedIndex = 0
        comb_wadifa.SelectedIndex = 0
        Try
            Partager.moi.remplir_List_employe()
            List_tous.Items.Clear()
            For Each khib As Employe In Partager.moi.List_employe
                List_tous.Items.Add(New ListViewItem(New String() {khib.cin.ToString, khib.Nom_a.ToString, khib.Prenom_a.ToString, khib.occupation.ToString, khib.Ville.ToString, khib.Date_emploi}))
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Partager.com_ville(comb_ville, "Comb_Ville", "Ville")
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        Try
            Partager.close()
            If (List_tous.SelectedIndices.Count > 0) Then
                MsgBox("عفوا لا يمكنك الإضافة، لإضافة موظف جديد عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
            Else
                If txt_mail.Text <> "" Then
                    bol = Partager.regEmail(txt_mail.Text)
                    If (bol = False) Then
                        txt_mail.Text = "البريد الإلكتروني خاطئ"
                        txt_mail.ForeColor = Color.Red
                        txt_mail.TextAlign = HorizontalAlignment.Right
                    End If
                End If

                If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "" And txt_cin.Text <> "" And bol = True) Then
                    If MsgBox("هل تريد فعلا إضافة هذا الموظف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة موظفين المكتب") = MsgBoxResult.Yes Then
                        Try

                            Dim cli As New Employe(1, txt_nom.Text, txt_prenom.Text, txt_3a2ili.Text, txt_chakhssi.Text, Date_nissance.Value, txt_adresse.Text, Date_tawdif.Value, comb_hala_ijtim.Text, comb_wadifa.Text, comb_ville.Text, txt_salaire.Text, txt_mail.Text, Partager.image(Pic_avoc), txt_molahadat.Text, Check_nachit.Checked, txt_cin.Text)
                            Partager.moi.Ajouter_employe(cli)
                            'l'objet en cours
                            Dim lastItem As Integer = Partager.moi.List_employe.Count - 1
                            cli = CType(Partager.moi.List_employe.Item(lastItem), Employe)

                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next


                            List_tous.Items.Clear()
                            For Each khib As Employe In Partager.moi.List_employe
                                List_tous.Items.Add(New ListViewItem(New String() {khib.cin, khib.Nom_a, khib.Prenom_a, khib.occupation, khib.Ville, khib.Date_emploi}))
                            Next
                            MsgBox("تمت إضافة الموظف بنجاح", MsgBoxStyle.Information, "نجاح")
                            Partager.vider(Me)
                        Catch ex As Exception
                            MsgBox(ex.Message)
                        End Try
                    End If
                Else
                    MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If txt_mail.Text <> "" Then
                bol = Partager.regEmail(txt_mail.Text)
                If (bol = False) Then
                    txt_mail.Text = "البريد الإلكتروني خاطئ"
                    txt_mail.ForeColor = Color.Red
                    txt_mail.TextAlign = HorizontalAlignment.Right
                End If
            End If

            If (txt_3a2ili.Text <> "" And txt_chakhssi.Text <> "" And txt_cin.Text <> "" And bol = True) Then

                If MsgBox("هل تريد فعلا تعديل معلومات هذا الموظف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل موظفي المكتب") = MsgBoxResult.Yes Then

                    Try
                        Partager.close()
                        Dim cli As New Employe(1, txt_nom.Text, txt_prenom.Text, txt_3a2ili.Text, txt_chakhssi.Text, Date_nissance.Value, txt_adresse.Text, Date_tawdif.Value, comb_hala_ijtim.Text, comb_wadifa.Text, comb_ville.Text, txt_salaire.Text, txt_mail.Text, Partager.image(Pic_avoc), txt_molahadat.Text, Check_nachit.Checked, txt_cin.Text)
                        Partager.moi.modifier_employe(cli, avo.Id)

                        cli.Id = avo.Id
                        If Partager.list_hatif.Count > 0 Then
                            cli.suprimmer_phone(avo.Id, "Tel")
                            For Each d As Phone In Partager.list_hatif
                                cli.Ajouter_phone(d)
                            Next
                        End If

                        List_tous.Items.Clear()
                        For Each khib As Employe In Partager.moi.List_employe
                            List_tous.Items.Add(New ListViewItem(New String() {khib.cin, khib.Nom_a, khib.Prenom_a, khib.occupation, khib.Ville, khib.Date_emploi}))
                        Next
                        MsgBox("عملية التعديل تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me)
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                End If

            Else
                MsgBox("أدخل البيانات من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Else
            MsgBox("اختر موظف من القائمة", MsgBoxStyle.Information, "خطأ في التعديل")
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Partager.close()
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذا الموظف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف موظفي المكتب") = MsgBoxResult.Yes Then
                Try
                    Dim clt As Employe = Partager.moi.List_employe(List_tous.SelectedIndices.Item(0))
                    Partager.moi.suprimmer_employe(avo.Id)

                    List_tous.Items.Clear()
                    For Each khib As Employe In Partager.moi.List_employe
                        List_tous.Items.Add(New ListViewItem(New String() {khib.cin, khib.Nom_a, khib.Prenom_a, khib.occupation, khib.Ville, khib.Date_emploi}))
                    Next

                    MsgBox("تمت عملية الحذف بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر موظف من القائمة", MsgBoxStyle.Information, "بحث")
        End If
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices

                avo = Partager.moi.List_employe(i)
                txt_nom.Text = avo.Nom
                txt_prenom.Text = avo.Prenom
                txt_3a2ili.Text = avo.Nom_a
                txt_chakhssi.Text = avo.Prenom_a
                Date_nissance.Value = avo.Date_naiss
                txt_adresse.Text = avo.Adress
                Date_tawdif.Value = avo.Date_emploi
                comb_hala_ijtim.Text = avo.Situation_social
                comb_wadifa.Text = avo.occupation
                comb_ville.Text = avo.Ville
                If avo.salaire = 0 Then
                    txt_salaire.Text = ""
                Else
                    txt_salaire.Text = avo.salaire
                End If
                txt_mail.Text = avo.email
                txt_molahadat.Text = avo.Remarque
                Check_nachit.Checked = avo.Actif
                txt_cin.Text = avo.cin
                Dim bytBLOBData() As Byte = avo.photo
                Dim stmBLOBData2 As New MemoryStream(bytBLOBData)
                Pic_avoc.Image = Image.FromStream(stmBLOBData2)
                avo.remplir_List_list_phone()
                For Each numhone As Phone In avo.list_phone
                    If numhone.Type = "Tel" Then
                        Partager.list_hatif.Add(numhone)
                    End If
                Next
            Next
            If List_tous.SelectedIndices.Count <= 0 Then
                Partager.vider(Me)
                Check_nachit.Checked = False
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_parc_photo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_parc_photo.Click
        With OpenF
            .FileName = ""
            .Title = "Ovrir la Photo"
            .RestoreDirectory = True
            .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            .Filter = "JPEG Files (*.JPG)|*.JPG"
            .FilterIndex = 1
        End With
        If OpenF.ShowDialog = Windows.Forms.DialogResult.OK Then
            Pic_avoc.Image = Image.FromFile(OpenF.FileName)
            Loc = OpenF.FileName
        End If
    End Sub

    Private Sub Btn_photo_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_photo_supp.Click
        Pic_avoc.Image = My.Resources.photo
    End Sub

    Private Sub txt_mail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_mail.Click
        If bol = False Then
            txt_mail.Text = ""
        End If
        bol = True
    End Sub

    Private Sub txt_mail_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_mail.KeyPress
        If e.KeyChar = ChrW(32) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txt_mail_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_mail.TextChanged
        txt_mail.ForeColor = Color.Black
        txt_mail.TextAlign = HorizontalAlignment.Left
    End Sub

    Private Sub txt_cin_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_cin.KeyPress
        If e.KeyChar = ChrW(32) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txt_cin_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_cin.TextChanged

    End Sub

    Private Sub txt_salaire_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_salaire.KeyPress
        Dim allowedChars As String = "1234567890,"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub txt_salaire_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_salaire.TextChanged

    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me)
        Check_nachit.Checked = False
        txt_3a2ili.Select()
    End Sub
End Class